﻿using Loyalty.Application;
using Loyalty.Core.Entities;
using Loyalty.Infrastructure.EntityFramework.Interfaces;
using Loyalty.Infrastructure.EntityFramework.Poco;
using Loyalty.Model.Poco;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Loyalty.Infrastructure.EntityFramework.Repositories
{
    public class CityRepository: ICityRepository
    {

        public readonly IDbSet<City> _Repository;
        public readonly IUnitOfWork _UnitOfWork;

        public CityRepository(IUnitOfWork uow)
        {
            _UnitOfWork = uow;
            _Repository = uow.DbSet<City>();
        }
        public ErrorExeptionsResult<City> Add(City Entity)
        {
            var ret = new ErrorExeptionsResult<City>();
            _Repository.Add(Entity);
            ret.SelectedItem = Entity;
            return ret;
        }

        public ErrorExeptionsResult<City> Delete(City Entity)
        {
            var ret = new ErrorExeptionsResult<City>();
            var item = _Repository.Find(Entity.Id);
            if (item == null)
            {
                ret.RequestState = RequestStates.Fail;
                ret.ErrorMessage = "اطلاعات یافت نشد";
            }
            else
            {
                _Repository.Remove(item);
                ret.SelectedItem = Entity;
            }
            return ret;
        }

        public ErrorExeptionsResult<City> Edit(City Entity)
        {
            var ret = new ErrorExeptionsResult<City>();
            var item = _Repository.Find(Entity.Id);
            if (item == null)
            {
                ret.RequestState = RequestStates.Fail;
                ret.ErrorMessage = "اطلاعات یافت نشد";
            }
            else
            {
                _Repository.Update(Entity,_UnitOfWork);
                ret.SelectedItem = item;
            }
            return ret;
        }

        public ErrorExeptionsResult<City> Get(long? id)
        {
            var ret = new ErrorExeptionsResult<City>();
            var item = _Repository.Find(id);
            if (item != null)
            {
                ret.SelectedItem = item;
            }
            return ret;
        }

        public ErrorExeptionsResult<City> GetAll(int? PageNumber, int? RowPerPage, DynamicSearchEngine Filters)
        {
            return GetAll(null, PageNumber, RowPerPage, Filters);
        }

        public ErrorExeptionsResult<City> GetAll(City item, DynamicSearchEngine Filters)
        {
            return GetAll(item, null, null, Filters);
        }

        public ErrorExeptionsResult<City> GetAll(DynamicSearchEngine Filters)
        {
            return GetAll(null, null, null, Filters);
        }

        public ErrorExeptionsResult<City> GetAll(City Entity, int? PageNumber, int? RowPerPage, DynamicSearchEngine Filters)
        {
            var query = _Repository.AsQueryable();
            if (Entity != null)
            {
                if (Entity.Id != 0)
                    query = query.Where(x => x.Id == Entity.Id);
                if (!string.IsNullOrEmpty(Entity.Title))
                    query = query.Where(x => x.Title.Contains(Entity.Title));


            }
            var ret = new ErrorExeptionsResult<City>();

            query = query.GetFilteringQuery(Filters);

            ret.RecordCount = query.LongCount();

            if (PageNumber != null && RowPerPage != null && PageNumber.Value >= 0 && RowPerPage.Value > 0)
                query = query.Skip(RowPerPage.Value * PageNumber.Value).Take(RowPerPage.Value);

            ret.CurrentItems = query.ToList();
            return ret;
        }

        public ErrorExeptionsResult<IncomingUse> GetIncomingUse(IncomingUse Entity)
        {
            var query = _Repository.AsQueryable();
            if (Entity != null)
            {
                if (Entity.Searchtype == SearchTypes.ById)
                {
                    if (Entity.Id != null && Entity.Id != 0)
                        query = query.Where(x => x.Id == Entity.Id);
                }
                else
                {
                    if (!string.IsNullOrEmpty(Entity.TableValue))
                        query = query.Where(x => x.Title.Contains(Entity.TableValue));
                }
            }
            var ret = new ErrorExeptionsResult<IncomingUse>();
            ret.CurrentItems = query.Select(
                c =>
                new IncomingUse()
                {
                    Description = c.Title,
                    Notes = c.Title,
                    Id = c.Id
                }).ToList();
            return ret;
        }
    }
}
